#!/usr/bin/python
import sys,time,socket,struct
param = sys.argv[1:]
IP,PACKAGE_SIZE,LOG = param
Mcache = []
Buffer = []
PACKAGE_SIZE = int(PACKAGE_SIZE)
PAYLOAD = PACKAGE_SIZE- 24
log = open(LOG+time.strftime('%Y%m%d%H%M%S')+'.txt','w')
class csnode:
	#print 'voce esta tentando conectar em '+ ip
	sNode = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sNode.connect((IP, 50007))
	sNode.send('PLAY')
	while 1:
		data = sNode.recv(PACKAGE_SIZE)
		dataRecv = time.strftime('%H:%M:%S')
		size_recv = len(data)
		if size_recv > 1024:
			id,chunk = struct.unpack('24s'+str(PAYLOAD)+'s',data)
			log.write(str(int(id))+';')
		        log.write(str(size_recv)+';')
			log.write('recv'+dataRecv+';')
			sys.stdout.write(chunk)
			dataRead = time.strftime('%H:%M:%S')
			log.write('read'+dataRead+';\r\n')
			
		else:
			log.write('pacotePerdido;')
			log.write(str(size_recv)+';\r\n')
			break
	sNode.close()	       
node = csnode()
